package com.huajx.armp.htxny.handler;

import com.huajx.core.handler.AbstractHandler;
import com.huajx.core.kit.AsyncHttpClientKit;
import com.huajx.core.message.RequestMessage;
import com.huajx.util.HexUtil;
import com.huajx.util.StrUtil;
import com.huajx.util.check.BCC;
import com.huajx.util.lang.Assert;
import com.huajx.util.map.MapUtil;
import org.apache.mina.core.session.IoSession;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Arrays;
import java.util.Map;

/**
 * @program: di-parent
 * @description: 统一处理
 * @author: lisa 叶新华
 * @create: 2020-03-26 09:39
 */
public abstract class AbstractHtxnyHandler extends AbstractHandler {
    protected final static Logger logger = LoggerFactory.getLogger(AbstractHtxnyHandler.class);

    @Override
    protected String getReturnPrefix() {
        return StrUtil.EMPTY;
    }

    @Override
    protected <T> T getMessage(Object msg) {
        if (logger.isInfoEnabled()) {
            printMessage(msg);
        }

        return null;
    }

//    protected String parseDate(byte[] bytes, int pos) {
//        StringBuilder sb = new StringBuilder();
//        byte temp;
//        for (int i = pos; i < pos + 6; i++) {
//            temp = bytes[i];
//            if (temp < 10) {
//                sb.append('0');
//            }
//            sb.append(bytes[i]);
//        }
//        return sb.toString();
//    }

//    protected byte[] startBytes(byte[] bytes) {
//        assertIndexOutOfBounds(bytes);
//
//        return Arrays.copyOfRange(bytes, 0, 24);
//    }

//    protected int packLen(byte[] bytes) {
//        assertIndexOutOfBounds(bytes);
//
//        return Integer.parseInt(HexUtil.bytesToHex(Arrays.copyOfRange(bytes, 22, 24)), 16);
//    }

//    protected byte bcc4Htxny(byte[] data, boolean hasBcc) {
//        return BCC.xor(data, 2, data.length - 1);
//    }
//
//    protected byte bcc(byte[] data) {
//        return BCC.xor(data);
//    }

    protected void assertIndexOutOfBounds(byte[] bytes) {
        if (bytes.length < 24) {
            throw new ArrayIndexOutOfBoundsException("包长度不够");
        }
    }

    /**
     * 响应输出
     *
     * @param session 会话
     * @param bytes   请求消息
     */
    protected void render(IoSession session, byte[] bytes) {
//        int len = 31;
//        int end = 30;

        byte[] res = Arrays.copyOfRange(bytes, 0, 31);
        res[3] = 0x01;


        res[22] = 0x00;
        res[23] = 0x06;

        res[30] = BCC.xor(res, 2, 30);

        session.write(res);
    }

    protected void asyncPost(String controllerKey, Map<String, Object> paras, AsyncHttpClientKit.StringFutureCallback callback) {
        controllerKey = getServerUrl(controllerKey);
//        logger.info(controllerKey);

        AsyncHttpClientKit.post(controllerKey, paras, content -> {
            if (callback == null) {
//                render(request.getSession(), bytes, content);
            } else {
                callback.success(content);
            }
        });
    }
    protected void asyncPost(String controllerKey, Map<String, Object> paras) {
        controllerKey = getServerUrl(controllerKey);
//        logger.info(controllerKey);

        AsyncHttpClientKit.post(controllerKey, paras, content -> {
        });
    }

    private final static String car_monitor_api = "/api/car/monitor";

    protected void postCarMonitor(byte[] bytes){
        Map<String, Object> params = MapUtil.newHashMap();

        params.put("carData", HexUtil.encodeHexStr(bytes));

        asyncPost(car_monitor_api, params);
    }
}
